<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <div class="tips">
            <i class="el-icon-info" />
            <span>本月: 社保在缴 公积金在缴</span>
          </div>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="goExcel">excel导入</el-button>
          <el-button type="danger" size="small" @click="exportExcel">excel导出</el-button>
          <el-button type="primary" size="small" @click="getaddList">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column width="60" label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="{row }">
              {{ formatEmployment(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间">
            <template #default="{ row }">
              {{ formatDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="200">
            <template #default="{ row }">
              <el-button type="text" size="small" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text" size="small" @click="getDetail(row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="delcolse(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          :total="total"
          :current-page="params.page"
          :page-size="params.size"
          layout="prev, pager, next"
          @current-change="pageChange"
        />
      </el-card>
      <add-employee :disget-list="addVisible" @closeList="setList" @setLists="fetchList" />
      <template>
        <el-dialog
          class="assign-role"
          title="分配角色"
          :visible="showRoleDialog"
          @close="closeRoleDialog"
          @open="openRoleDialog"
        >
          <!-- 这里准备复选框 -->
          <el-checkbox-group v-model="checked">
            <!-- 注意：label决定当前选中的值 -->
            <el-checkbox v-for="item in ListId" :key="item.id" :label="item.id">
              {{ item.name }}
            </el-checkbox>
          </el-checkbox-group>
          <template #footer>
            <el-button type="primary" size="small" @click="submitList">确定</el-button>
            <el-button size="small" @click="closeRoleDialog">取消</el-button>
          </template>
        </el-dialog>
      </template>
    </div>
  </div>
</template>
<script>
import dayjs from 'dayjs'
import { getEmployeeList, delEmployeeList, assignRoles } from '@/api/employee'
import addEmployee from '@/views/Employee/componets/add-employee.vue'
import { getExportData } from '@/utils/excel'
import { getrole } from '@/api/setting'
export default {
  components: {
    addEmployee
  },
  data() {
    return {
      list: [],
      ListId: [],
      checked: [],
      curId: null,
      params: {
        page: 1,
        size: 5
      },
      total: 0,
      addVisible: false,
      showRoleDialog: false
    }
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    async fetchList() {
      const res = await getEmployeeList(this.params)
      this.list = res.rows
      this.total = res.total
    },
    // 处理聘用格式化
    formatEmployment(value) {
      const map = {
        1: '正式',
        2: '非正式',
        4: '其他'
      }
      return map[value]
    },
    // 处理时间
    formatDate(value) {
      return dayjs(value).format('YYYY-MM-DD')
    },
    // 分页
    pageChange(page) {
      this.params.page = page
      this.fetchList()
    },
    // 打开弹窗
    getaddList() {
      this.addVisible = true
    },
    // 关闭弹窗
    setList() {
      this.addVisible = false
    },
    // 删除
    async delcolse(id) {
      this.$confirm('确定要删除吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployeeList(id)
        this.$message.success('删除成功')
        this.fetchList()
      }).catch(() => {
      })
    },
    // 跳转到导入
    goExcel() {
      this.$router.push('/import')
    },
    // 导出
    exportExcel() {
      import('@/vendor/Export2Excel').then(async excel => {
        const res = await getEmployeeList(this.params)
        const headerRelation = {
          '姓名': 'username',
          '手机号': 'mobile',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '聘用形式': 'formOfEmployment',
          '部门': 'departmentName'
        }
        const { data } = getExportData(res.rows, headerRelation)
        excel.export_json_to_excel({
          header: Object.keys(headerRelation),
          data: data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })
      })
    },
    goDetail(id) {
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    },
    getDetail(id) {
      this.showRoleDialog = true
      this.curId = id
    },
    closeRoleDialog() {
      this.showRoleDialog = false
    },
    async openRoleDialog() {
      const res = await getrole({
        page: 1,
        pagesize: 100
      })
      this.ListId = res.rows
      console.log(this.ListId)
    },
    async  submitList() {
      await assignRoles({
        id: this.curId,
        roleIds: this.checked
      })
      this.$message.success('分配成功')
      this.closeRoleDialog()
    }

  }
}
</script>
